package com.easyedu.dean.service;

import com.easyedu.core.entity.DbError;
import com.easyedu.core.entity.DeanServiceError;
import com.easyedu.core.entity.po.Course;
import com.easyedu.core.entity.po.Institute;
import com.easyedu.core.entity.po.OpenCourse;
import com.easyedu.core.entity.po.StudentSelect;
import com.easyedu.core.exception.EasyEduException;
import com.easyedu.core.dao.CourseDao;
import com.easyedu.core.dao.InstituteDao;
import com.easyedu.core.dao.StudentSelectDao;
import com.easyedu.dean.entity.dto.request.ReqCourse;
import com.easyedu.core.entity.query.CourseQuery;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lmr
 **/
@Service
public class CourseService {

     private final CourseDao        courseDao;
     private final StudentSelectDao studentSelectDao;
     private final InstituteDao     instituteDao;
     private final InstituteService instituteService;

     public CourseService(CourseDao courseDao, StudentSelectDao studentSelectDao, InstituteDao instituteDao,
                          InstituteService instituteService) {

          this.courseDao = courseDao;
          this.studentSelectDao = studentSelectDao;
          this.instituteDao = instituteDao;
          this.instituteService = instituteService;
     }

     public List<Course> getCourse(CourseQuery courseQuery) {

          return courseDao.getCourse(courseQuery);
     }

     @Transactional(rollbackFor = Exception.class)
     public long createCourse(ReqCourse reqCourse) {

          long openInstituteId = reqCourse.getOpenInstituteId();
          String openInstituteName = reqCourse.getOpenInstituteName();
          String courseCode = reqCourse.getCourseCode();
          String courseName = reqCourse.getCourseName();
          float credit = reqCourse.getCredit();
          Course course = new Course(0, reqCourse.getOpenInstituteId(), reqCourse.getOpenInstituteName(),
                  reqCourse.getCourseCode(), reqCourse.getCourseName(), reqCourse.getCourseNature(),
                  reqCourse.getHaveInClassExperiment(), reqCourse.getExaminationMethod(), reqCourse.getCredit(),
                  reqCourse.getGradeType(), reqCourse.getNote(), null, null);
          Institute instituteById = instituteDao.getInstituteById(openInstituteId);
          if (instituteById == null) {
               throw new EasyEduException(DeanServiceError.ERR_INSTITUTE_EXIST,
                       "新增加的课程应属于一个已存在的学院，请确认是否存在一个id为 " + openInstituteId + "的学院");
          }
          instituteService.hasInstitute(openInstituteId, openInstituteName);
          Course courseByCode = courseDao.getCourseByCode(courseCode);
          if (courseByCode != null && !courseByCode.getCourseName().equals(courseName)) {
               throw new EasyEduException(DeanServiceError.ERR_COURSE_CODE_EXIST,
                       "此课程代码下已经存在一个课程，同一课程代码只能存在一种课程，但是学分可以不同。");
          }

          if ((courseByCode != null ? courseByCode.getCredit() : 0) == credit) {
               throw new EasyEduException(DeanServiceError.ERR_BASE_COURSE_CODE_EXIST);
          }
          Integer integer = courseDao.createCourse(course);
          if (integer != 1) {
               throw new EasyEduException(DbError.Err_INSERT_DB_ERROR);
          }
          return course.getCourseId();
     }

     public List<OpenCourse> getApprovalCourse() {

          return studentSelectDao.getNeedApprovalCourse();
     }

     public List<StudentSelect> getCourseGrade(Long openCoursesId) {

          return studentSelectDao.getCourseGrade(openCoursesId);
     }

     public String passApprovedCourse(Long openCoursesId) {

          int number = studentSelectDao.passApprovedCourse(openCoursesId);
          return "审批通过" + number + "个学生成绩";
     }

     public boolean hasCourse(long courseId) {

          Course courseById = courseDao.getCourseById(courseId);
          return courseById != null && courseById.getCourseId() != 0;
     }
}
